package com.example.login.Controller;

import com.example.login.Mapper.CollectMap;
import com.example.login.Mapper.GoodsMap;
import com.example.login.Pojo.*;
import com.example.login.Service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.swing.plaf.synth.SynthTextAreaUI;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;

@Controller
public class CollectController {
    //添加进购物车
    @Autowired
    CollectService cs;
    @Autowired
    GoodsMap goodsMap;
    @RequestMapping("/user/collect/{id}")
    public String AddCollect(@PathVariable("id") int id,
                             HttpSession session,
                             RedirectAttributes attributes) {
        User user = (User) session.getAttribute("user");
        int user_id = user.getId();
        Goods good = cs.AddCollect(id, user_id);
        String cid = goodsMap.determine(id);
        attributes.addFlashAttribute("msg","收藏成功！");
        switch (cid) {
            case "MobilePhone":
                List<mobileGoods> mobileGoods = goodsMap.getMobileGoodsById(id);
                attributes.addFlashAttribute("mobiles", mobileGoods);
                return "redirect:/mobileInformation";
            case "MP3":
                List<mp3Goods> mp3Goods = goodsMap.getMp3GoodsById(id);
                attributes.addFlashAttribute("mp3s", mp3Goods);
                return "redirect:/mp3Information";
            case "PC":
                List<PCGoods> pcGoods = goodsMap.getPCGoodsById(id);
                System.out.println(pcGoods);
                attributes.addFlashAttribute("pcs", pcGoods);
                return "redirect:/pcInformation";
            default:
                return "/error";
        }
    }
    //进入收藏夹
    @RequestMapping("/user/collect")
    public String InCollect(HttpSession session, Model model)
    {
        User user=(User)session.getAttribute("user");
        int user_id =user.getId();
        ArrayList<Goods> goods=cs.getGoods(user_id);
        model.addAttribute("goods",goods);
        System.out.println("1");
        System.out.println(goods);
        return "收藏夹";
    }
}
